// pages/cart/cart.js
Page(
  Object.assign({},
  {
    data: {
      isAllSelect: false,
      isSelectArray:[],
      totalMoney: 0,
      // 商品详情介绍
      carts: [
        {
          pic: "http://mz.djmall.xmisp.cn/files/product/20161201/148058328876.jpg",
          name: "日本资生堂洗颜",
          price: 200,
          // 数据设定
          count: {
            quantity: 2,
            min: 1,
            max: 20
          },
        },
        {
          pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148058301941.jpg',
          name: "倩碧焕妍活力精华露",
          price: 340,
          // 数据设定
          count: {
            quantity: 1,
            min: 1,
            max: 20
          },
        },
        {
          pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/14805828016.jpg',
          name: "特效润肤露",
          price: 390,
          // 数据设定
          count: {
            quantity: 3,
            min: 1,
            max: 20
          },
        },
        {
          pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148058228431.jpg',
          name: "倩碧水嫩保湿精华面霜",
          price: 490,
          // 数据设定
          count: {
            quantity: 1,
            min: 1,
            max: 20
          },
        },
        {
          pic: 'http://mz.djmall.xmisp.cn/files/product/20161201/148057953326.jpg',
          name: "兰蔻清莹柔肤爽肤水",
          price: 289,
          // 数据设定
          count: {
            quantity: 10,
            min: 1,
            max: 20
          },
        },
        {
          pic: "http://mz.djmall.xmisp.cn/files/product/20161201/148057921620_middle.jpg",
          name: "LANCOME兰蔻小黑瓶精华",
          price: 230,
          // 数据设定
          count: {
            quantity: 1,
            min: 1,
            max: 20
          },
        },
      ],
    },
    onShow(){
      for (let i = 0; i < this.data.carts.length; i++) {
        const element = this.data.carts[i];
        this.data.isSelectArray.push({
          isSelect:false
        })
      }
    },
    //勾选事件处理函数  
    switchSelect: function (e) {
      var Allprice = 0, i = 0;
      let id = e.target.dataset.id,
      index = parseInt(e.target.dataset.index);
      this.setData({
        [`isSelectArray[${index}].isSelect`]: !this.data.isSelectArray[index].isSelect
      })

      //价钱统计
      if (this.data.isSelectArray[index].isSelect) {
        this.data.totalMoney = this.data.totalMoney + this.data.carts[index].price;
      }else {
        this.data.totalMoney = this.data.totalMoney - this.data.carts[index].price;
      }


      //是否全选判断
      for (i = 0; i < this.data.carts.length; i++) {
        Allprice = Allprice + this.data.carts[i].price;
      }
      if (Allprice == this.data.totalMoney) {
        this.data.isAllSelect = true;
      }
      else {
        this.data.isAllSelect = false;
      }
      this.setData({
        carts: this.data.carts,
        totalMoney: this.data.totalMoney,
        isAllSelect: this.data.isAllSelect,
      })
    },


    //全选
    allSelect: function (e) {
      //处理全选逻辑
      let i = 0;
      if (!this.data.isAllSelect) {
        for (i = 0; i < this.data.carts.length; i++) {
          this.setData({
            [`isSelectArray[${i}].isSelect`]: true
          })
          this.data.totalMoney = this.data.totalMoney + this.data.carts[i].price;
        }
      }
      else {
        for (i = 0; i < this.data.carts.length; i++) {
          this.setData({
            [`isSelectArray[${i}].isSelect`]: false
          })
        }
        this.data.totalMoney = 0;
      }
      this.setData({
        carts: this.data.carts,
        isAllSelect: !this.data.isAllSelect,
        totalMoney: this.data.totalMoney,
      })
    },
    // 去结算
    toBuy() {
      wx.showToast({
        title: '去结算',
        icon: 'success',
        duration: 3000
      });
      this.setData({
        showDialog: !this.data.showDialog
      });
    },
  }));